<?php
namespace Libs\WeiXin;
use Libs\Curl\Curl;
use Think\Cache;
//会员管理
class User extends Weixin
{
    public $Curl ;

    public function __construct()
    {
        parent::__construct();
    }
    //设置备注名
    public function setRemark($param)
    {
        $Curl =new Curl();
        $url = $this->apiUrl . '/cgi-bin/user/info/updateremark?access_token=' . $this->getAccessToken();

        $content = $Curl->post($url, urldecode(json_encode($this->urlencodeArray($param))));

        $result = json_decode($content, true);

        return $this->get($result);
    }

    //获取用户基本信息
    public function getUserInfo($openid)
    {
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.  session('user_access_token').'&openid='.$openid.'&lang=zh_CN';
        //$url = $this->apiUrl . '/cgi-bin/user/info?openid='.$openid.'&lang=zh_CN&access_token=' . $this->getAccessToken();
        $Curl =new Curl();
        $content = $Curl->get($url);
        $result = json_decode($content, true);
        return $this->get($result);
    }

    //获取用户基本信息
    public function getWxUserInfo($openid)
    {
        //$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.  session('user_access_token').'&openid='.$openid.'&lang=zh_CN';
        $url = $this->apiUrl . '/cgi-bin/user/info?openid='.$openid.'&lang=zh_CN&access_token=' . $this->getAccessToken();
        $Curl =new Curl();
        $content = $Curl->get($url);
        $result = json_decode($content, true);
        return $this->get($result);
    }
    
    
    //批量获取用户基本信息
    public function getUserInfoLists($param)
    {
        $url = $this->apiUrl . '/cgi-bin/user/info/batchget?access_token=' . $this->getAccessToken();
        $Curl =new Curl();
        $content = $Curl->post($url, urldecode(json_encode($this->urlencodeArray($param))));

        $result = json_decode($content, true);

        return $this->get($result);
    }

    //获取用户列表
    public function getUserLists($next_openid='')
    {
        $url = $this->apiUrl . "/cgi-bin/user/get?access_token={$this->access_token}&next_openid={$next_openid}";
        $Curl =new Curl();
        $content = $Curl->get($url);

        $result = json_decode($content, true);

        return $this->get($result);
    }

    /*
	* 静默获取OpenID
	* $uri:要跳转至的url
	*/
    public function getUserOpenid($uri)
    {
        $Curl =new Curl();
        if(!isset($uri)){
            return false;
            exit;
        }
        //if(!session('wx_openid')){
//            if(isset($_GET['code'])){
//                //code参数已有，获取openid;
//                $url_get='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.APPID.'&secret='.APPSECRET.'&code='.$_GET['code'].'&grant_type=authorization_code';
//                $res=json_decode($Curl->get($url_get));
//                return $res->openid;
//            }else{
                //无code参数，先获取code
                $uri=urlencode($uri);//这里需要urlencode一下
                $redurl='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.APPID.'&redirect_uri='.$uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
                header("location:".$redurl);
                exit;
           // }
        //}
    }
}